<template>
  <div class="fillcontain">

    <div class="table_container">
      <div class="search-box">
        <el-row :gutter="20">
          <el-col :md="8">
            <el-input
              v-model="searchInpt"
              placeholder="输入ID/昵称/手机号"
              clearable
              @keyup.enter.native="searchUser()"
            >
              <el-button
                type="primary"
                @click="searchUser()"
                icon="el-icon-search"
                slot="append"
                :loading="searchLoading"
              >搜索</el-button>
            </el-input>
          </el-col>
          <el-col :md="4">

          </el-col>
          <el-col :md="6">
            <el-select
              v-model="thousandPlanStatusValue"
              placeholder="请选择"
              @change="changeThousandPlanStatusValue()"
            >
              <el-option
                v-for="item in thousandPlanStatus"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-col>
          <el-col :md="8">
            <div class="last">
              <!-- <el-button type="text" @click="goMessageTemplate()">模版设置</el-button> -->
              <!-- <el-button type="text" @click="processingRecords()">处理记录</el-button> -->
            </div>
          </el-col>
        </el-row>
      </div>

      <el-table
        v-loading="tableloading"
        :data="tableData"
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column
          property="userId"
          label="id"
          align="center"
          fixed
        >
        </el-table-column>
        <el-table-column
          property="nickName"
          label="用户昵称"
          align="center"
        >
        </el-table-column>
        <el-table-column
          property="userName"
          label="手机号"
          width="150"
          align="center"
        >
        </el-table-column>
        <el-table-column
          property="detectionTime"
          :formatter="formatterTime"
          label="最后检测时间"
          width="110"
          align="center"
        >
        </el-table-column>
        <el-table-column
          label="状态"
          align="center"
        >
          <template slot-scope="scope">
            <span
              class="lz-span lz-primary"
              v-if="scope.row.customerServiceHandle == 0 "
            >未操作</span>
            <span
              class="lz-span lz-success"
              v-else-if="scope.row.customerServiceHandle == 1"
            >无异常</span>
            <span
              class="lz-span lz-danger"
              v-else-if="scope.row.customerServiceHandle == 2"
            >已电话</span>
            <span
              class="lz-span lz-danger"
              v-else-if="scope.row.customerServiceHandle == 3"
            >已app</span>
            <span
              class="lz-span lz-danger"
              v-else-if="scope.row.customerServiceHandle == 4"
            >已短信</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="thousandPlan"
          label="千人计划"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              class="lz-success"
              type="text"
              @click="planStatus(scope.row)"
              size="small"
              v-if="scope.row.status == 0"
            >已参与</el-button>
            <el-button
              class="lz-danger"
              type="text"
              @click="planStatus(scope.row)"
              size="small"
              v-else
            >未参与</el-button>
          </template>
        </el-table-column>
        <el-table-column
          property=""
          label="曲线"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              @click="goGraph(scope.row)"
              type="text"
              size="small"
            >查看</el-button>
          </template>
        </el-table-column>

        <el-table-column
          label="处理操作"
          align="center"
          width="200"
        >
          <template slot-scope="scope">
            <el-button
              @click="changeCustomerHandleStatus(scope.row,2)"
              type="text"
              size="small"
              v-if="scope.row.customerServiceHandle == 0 && scope.row.status == 0"
            >电话通知</el-button>
            <el-button
              @click="sendMessage(scope.row,2)"
              type="text"
              size="small"
              v-if="scope.row.customerServiceHandle == 0 && scope.row.status == 1"
            >APP通知</el-button>
            <el-button
              @click="sendMessage(scope.row,1)"
              type="text"
              size="small"
              v-if="scope.row.customerServiceHandle == 0 && scope.row.status == 1"
            >短信通知</el-button>
            <el-button
              @click="changeCustomerHandleStatus(scope.row,1)"
              type="text"
              size="small"
              v-if="scope.row.customerServiceHandle == 0"
            >无异常</el-button>
            <el-button
              @click="changeCustomerHandleStatus(scope.row,0)"
              type="text"
              size="small"
              v-if="scope.row.customerServiceHandle !== 0"
            >改为未操作</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div
        class="Pagination"
        style="text-align: left;margin-top: 10px;"
      >
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="limit"
          layout="total, prev, pager, next"
          :total="count"
        >
        </el-pagination>
      </div>
    </div>

    <el-dialog
      title="发送消息"
      :visible.sync="showSendMessage"
      width="50%"
      center
    >
      <el-radio-group
        v-for="item in SMStemplateData"
        :key="item.templateCode"
        v-model="SMStemplateValue"
      >
        <ul class="SMS-ul">
          <li v-if="item.role == 1">
            <el-tooltip
              class="item"
              effect="dark"
              :content="item.context"
              placement="top-start"
            >
              <el-radio :label="item.templateCode">
                {{item.context}}
              </el-radio>
            </el-tooltip>
          </li>
        </ul>
      </el-radio-group>
      <p>
        推送用户:
      </p>
      <p>
        用户ID:{{sendMessageData.userId}} 手机号:{{sendMessageData.userName}} 昵称:{{sendMessageData.nickName}}
      </p>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="showSendMessage = false">取 消</el-button>
        <el-button
          type="primary"
          @click="doSendMessage()"
        >发 送</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import headTop from "../components/headTop";
import {
  getUnusualInfo,
  delMac,
  searchUser,
  thousandPlanStatus,
  customerStatus,
  customerFind,
  fetchGetSMSTemplate,
  fetchSendSMS,
  fetchYouMengTemplateData,
  fetchYouMengPushData
} from "@/api/getData";
import { ip1Conf } from "@/config/env";
import { formatDateTime } from "@/util/common";
export default {
  data() {
    return {
      tableData: [
        {
          userId: 818,
          userName: null,
          nickName: "mary娜",
          detectionTime: 1535931067000,
          customerServiceHandle: 2,
          backgroundHandle: 2,
          remark: null
        },
        {
          userId: 780,
          userName: null,
          nickName: " 敏",
          detectionTime: 1535929120000,
          customerServiceHandle: 2,
          backgroundHandle: 2,
          remark: null
        }
      ],
      count: 0,
      limit: 20,
      currentPage: 1,
      searchInpt: "",
      searchLoading: false,
      tableloading: true,
      thousandPlanStatus: [
        {
          value: "",
          label: "全部"
        },
        {
          value: "0",
          label: "已参加"
        },
        {
          value: "1",
          label: "未参加"
        }
      ],
      thousandPlanStatusValue: "",
      customerHandleDialog: false,
      //默认选择
      customerHandleStatus: "",
      //发送消息
      sendMsgBox: false,
      sendMsgData: {
        userId: "",
        msgConstent: ""
      },
      //弹框
      showSendMessage: false,
      sendMessageData: {},
      SMStemplateData: [],
      SMStemplateValue: "",
      SMStemplateStatus: "",
      sendChecked: true
    };
  },
  components: {
    headTop
  },
  mounted() {
    this.getUnusualInfo();
  },
  render: {},
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      if (this.searchInpt !== "") {
        const page = "1";
        this.searchUser(page);
      } else {
        this.getUnusualInfo();
      }
    },
    async getUnusualInfo() {
      this.tableloading = true;
      const Users = await getUnusualInfo({
        pageSize: this.limit,
        pageNo: this.currentPage,
        thousandPlanStatus: this.thousandPlanStatusValue
      });
      this.tableloading = false;
      this.tableData = [];
      this.count = Users.data.total;
      if (Users.data.list) {
        Users.data.list.forEach(item => {
          const tableData = {};
          tableData.userId = item.userId;
          tableData.userName = item.userName;
          tableData.nickName = item.nickName;
          tableData.detectionTime = item.detectionTime;
          tableData.prePregnant = item.prePregnant;
          tableData.count = item.count;
          tableData.physiologicalDataId = item.physiologicalDataId;
          tableData.status = item.status;
          tableData.customerServiceHandle = item.customerServiceHandle;
          this.tableData.push(tableData);
        });
      }
    },
    download(data) {
      location.href = ip1Conf + "/users/excel?userId=" + e.id;
    },
    async customerHandleData(data, i) {
      let radioStatus = "";
      if (i == 5) {
        radioStatus = 0;
      } else {
        radioStatus = i;
      }
      const res = await customerStatus({
        physiologicalDataId: data.physiologicalDataId,
        customerStatus: radioStatus
      });
      if (res.status == "200") {
        this.$message({
          type: "success",
          message: "修改成功"
        });
        this.getUnusualInfo();
      } else {
        this.$message({
          type: "error",
          message: res.msg
        });
      }
    },
    changeCustomerHandleStatus(data, i) {
      let confirmText = "";
      if (i == 0) {
        confirmText = "该条记录已处理，确定修改为未操作？";
      } else if (i == 1) {
        confirmText = "确定本次检测无异常？";
      } else if (i == 2) {
        confirmText = "确定已电话通知？";
      }

      this.$confirm(confirmText, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.customerHandleData(data, i);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    async planStatusData(data) {
      let statusData;
      if (data.status == null || data.status == 0) {
        statusData = 1;
      } else {
        statusData = 0;
      }
      const res = await thousandPlanStatus({
        thousandPlanStatus: statusData,
        userId: data.userId,
        physiologicalDataId: data.physiologicalDataId
      });

      if (res.status == 200) {
        this.$message({
          type: "success",
          message: "已修改!"
        });
        this.getUnusualInfo();
      } else {
        this.$message({
          type: "info",
          message: "修改失败!"
        });
      }
    },
    planStatus(data) {
      console.log(data);
      let planText = "";
      if (data.status) {
        planText = "确认将此用户改为已参加？";
      } else {
        planText = "确认将此用户改为未参加？";
      }
      this.$confirm(planText, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.planStatusData(data);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    goGraph(data) {
      this.$router.push({
        path: "pregnancyEcharts",
        query: { userId: data.userId }
      });
    },
    formatterTime(row) {
      if (row.detectionTime) {
        return formatDateTime(row.detectionTime);
      } else {
        return "";
      }
    },
    thousandStatus(value, row) {
      return row.status == value;
    },
    async searchUser(page) {
      if (page !== "1") {
        this.currentPage = 1;
      }
      this.searchLoading = true;
      this.tableloading = true;
      const searchUserInfo = await customerFind({
        pageSize: this.limit,
        pageNo: this.currentPage,
        key: this.searchInpt
      });
      this.searchLoading = false;
      this.tableloading = false;
      this.tableData = [];
      this.count = searchUserInfo.data.total;
      if (searchUserInfo.data.list) {
        searchUserInfo.data.list.forEach(item => {
          const tableData = {};
          tableData.userId = item.userId;
          tableData.userName = item.userName;
          tableData.nickName = item.nickName;
          tableData.detectionTime = item.detectionTime;
          tableData.prePregnant = item.prePregnant;
          tableData.count = item.count;
          tableData.physiologicalDataId = item.physiologicalDataId;
          tableData.status = item.status;
          tableData.customerServiceHandle = item.customerServiceHandle;
          this.tableData.push(tableData);
        });
      }
    },
    changeThousandPlanStatusValue() {
      this.getUnusualInfo();
    },
    sendMsg(data) {
      this.$router.push({
        path: "sendMsg",
        query: { userId: data.physiologicalDataId, userName: data.userName }
      });
    },
    processingRecords() {},
    goMessageTemplate() {
      this.$router.push({ path: "serviceMessageTemplate", query: {} });
    },
    async sendMessage(data, i) {
      this.SMStemplateStatus = i;
      if (i == "1") {
        const SMStemplate = await fetchGetSMSTemplate();
        this.SMStemplateData = [];
        if (SMStemplate.status == "200") {
          SMStemplate.data.forEach(item => {
            const tableData = {};
            tableData.context = item.templateContext;
            tableData.templateCode = item.templateCode;
            tableData.role = item.role;
            this.SMStemplateData.push(tableData);
          });
          this.showSendMessage = true;
          this.sendMessageData = {};
          this.sendMessageData.userId = data.userId;
          this.sendMessageData.userName = data.userName;
          this.sendMessageData.nickName = data.nickName;
          this.sendMessageData.physiologicalDataId = data.physiologicalDataId;
        }
      } else if (i == "2") {
        const SMStemplate = await fetchYouMengTemplateData();
        this.SMStemplateData = [];
        if (SMStemplate.status == "200") {
          SMStemplate.data.forEach(item => {
            const tableData = {};
            tableData.context = item.templateContext;
            tableData.templateCode = item.id;
            tableData.role = item.role;
            this.SMStemplateData.push(tableData);
          });
          this.showSendMessage = true;
          this.sendMessageData = {};
          this.sendMessageData.userId = data.userId;
          this.sendMessageData.userName = data.userName;
          this.sendMessageData.nickName = data.nickName;
          this.sendMessageData.physiologicalDataId = data.physiologicalDataId;
        }
      }
    },
    async doSendMessage() {
      if (this.sendChecked) {
        if (this.SMStemplateValue == "") {
          this.$message({
            type: "error",
            message: "请选择短信模版"
          });
          return;
        }
        this.sendChecked = false;
        if (this.SMStemplateStatus == "1") {
          const res = await fetchSendSMS({
            recNum: this.sendMessageData.userName,
            templateCode: this.SMStemplateValue,
            physiologicalDataId: this.sendMessageData.physiologicalDataId,
            chooseUser: "3"
          });
          this.sendChecked = true;
          this.showSendMessage = false;
          if (res.status == "200") {
            this.getUnusualInfo();
            this.SMStemplateValue = "";
            this.$message({
              type: "success",
              message: "发送成功"
            });
          } else {
            this.showSendMessage = false;
            this.$message({
              type: "error",
              message: "发送失败"
            });
          }
        } else if (this.SMStemplateStatus == "2") {
          const res = await fetchYouMengPushData({
            userId: this.sendMessageData.userId,
            pushMessageTemplateId: this.SMStemplateValue,
            physiologicalDataId: this.sendMessageData.physiologicalDataId,
            chooseUser: "3"
          });
          if (res.status == "200") {
            this.showSendMessage = false;
            this.getUnusualInfo();
            this.SMStemplateValue = "";
            this.$message({
              type: "success",
              message: "发送成功"
            });
          } else {
            this.showSendMessage = false;
            this.$message({
              type: "error",
              message: "发送失败"
            });
          }
        }
      }
    }
  },
  watch: {
    $route() {
      this.getUnusualInfo();
    }
  }
};
</script>

<style lang="less">
@import "../style/mixin";
.search-box {
  padding: 20px 0;
}
.SMS-ul {
  li {
    width: 400px;
    margin: 10px 0;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
.sendMagin {
  width: 400px;
  margin: 0 auto;
}
.last {
  text-align: right;
}
</style>
